package com.gridmobi.xrtb.routers.bidding;

import com.gridmobi.xrtb.domain.dsp.DspTrackLog;
import com.gridmobi.xrtb.domain.dsp.common.enums.ExchangesE;
import com.gridmobi.xrtb.dspcounter.DspCounterHandler;
import com.gridmobi.xrtb.handler.Mailer;
import com.gridmobi.xrtb.handler.monitor.RtbStatus;
import com.gridmobi.xrtb.util.RtbUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@RequestMapping("/")
public class WinNoticeController {
    private static final Logger logger = LoggerFactory.getLogger(WinNoticeController.class);
    private static final Logger winlog = LoggerFactory.getLogger("winlog");
    private static final Logger errorlogger = LoggerFactory.getLogger("error");
    private double defaultPrice = 5.0;
    private AtomicInteger errorcounter = new AtomicInteger();
    @Autowired
    private Mailer mailer;
    @Autowired
    DspCounterHandler dspCounterHandler;

    @RequestMapping(value = "dspwin", method = {RequestMethod.POST, RequestMethod.GET})
    public void win(@RequestParam Map<String, String> args, HttpServletRequest request, HttpServletResponse response) {
        winlog.warn(request.getQueryString());

        try {
            LocalDateTime now = LocalDateTime.now();
            DspTrackLog trackArgs = null;
            String bidid = args.get("bidid");


            if (bidid != null) {
                trackArgs = RtbUtil.packTrackArgs(now, args, request);
                if (trackArgs.getCid() == null) {
                    trackArgs.setCid(request.getParameter("cid"));
                }
                dspCounterHandler.countWin(Integer.parseInt(trackArgs.getCid()), trackArgs.getCrid(), trackArgs.getGeo(), trackArgs.getAppid(), trackArgs.getOs(), ExchangesE.getCode(trackArgs.getChannel()));
                /*
                default cpm
                 */
                double price = 0f;
                try {
                    price = Double.parseDouble(trackArgs.getPrice());
                } catch (Exception e) {

                    trackArgs.setPrice(String.valueOf(defaultPrice));
                    price = defaultPrice;
                }

                double actprice = price / 1000;

                dspCounterHandler.countCosts(Integer.parseInt(trackArgs.getCid()), trackArgs.getCrid(), trackArgs.getGeo(), trackArgs.getAppid(), trackArgs.getOs(), actprice, ExchangesE.getCode(trackArgs.getChannel()));


            }


        } catch (Exception e) {
            errorlogger.error("WINERROR", e);
            int i = errorcounter.incrementAndGet();
            if (i % 10 == 0) {
                RtbStatus.BID_LOCK_BY_WINNOTICE_ERROR = true;
                mailer.sendErrorMail("RTB WIN NOTICE ERROR:", "" + request.getQueryString());
            }
        }
    }
}
